package com.example.demo.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.common.Result;
import com.example.demo.entity.EoExecutePeople;
import com.example.demo.mapper.EoMapper;
import com.example.demo.mapper.ExecutePeopleMapper;
import com.example.demo.utils.SMSUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/sms")
public class SMSController {

    @Resource
    ExecutePeopleMapper executePeopleMapper;

    @Resource
    EoMapper eoMapper;


    @PostMapping("/send/{id}")
    public Result<?> send(@PathVariable Long id){
        LambdaQueryWrapper<EoExecutePeople> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(EoExecutePeople::getEoId, id);
        List<EoExecutePeople> people = executePeopleMapper.selectList(wrapper);

        String activity = eoMapper.selectById(id).getName();
        JSONArray phoneNumber = new JSONArray();
        JSONArray sign = new JSONArray();
        JSONArray param = new JSONArray();
        people.forEach(item -> {
            phoneNumber.add(item.getPhone());
            sign.add("宁波国际会议中心");
            JSONObject object = new JSONObject();
            object.set("name", item.getName());
            object.set("activity", activity);
            param.add(object);
        });
//        return Result.success(SMSUtil.sendBatch(phoneNumber.toString(), sign.toString(), param.toString()));
        String res = SMSUtil.sendBatch(phoneNumber.toString(), sign.toString(), param.toString());
        return Result.success("200", res);
    }
}
